﻿using Publicuse.Entity.AttributeUtil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Util
{
    /// <summary>
    /// 业务映射模型
    /// </summary>
    public class TableAtributeUtil
    {
        /// <summary>
        /// 获取表架构名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetTableSchema<T>()
        {
            string SchemaName = null;
            Type type = typeof(T);
            var infos = type.GetCustomAttributes(typeof(SchemaAttribute), true);
            foreach (var info in infos)
            {
                SchemaAttribute myTable = info as SchemaAttribute;
                if (myTable != null)
                {
                    SchemaName = myTable.GetName;
                }
            }
            return SchemaName;
        }

        /// <summary>
        /// 获取表名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetTable<T>()
        {
            string tableName = null;
            Type type = typeof(T);
            var infos = type.GetCustomAttributes(typeof(DBTableAttribute), true);
            foreach (var info in infos)
            {
                DBTableAttribute myTable = info as DBTableAttribute;
                if (myTable != null)
                {
                    tableName = myTable.GetMappingName();
                }
            }
            return tableName;
        }

        /// <summary>
        /// 获取表主键名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetPrimaryKey<T>()
        {
            string key = null;
            Type type = typeof(T);
            foreach (var pi in type.GetProperties())
            {
                var infos = pi.GetCustomAttributes(typeof(PrimaryKeyAttribute), true);
                foreach (var info in infos)
                {
                    PrimaryKeyAttribute primaryKey = info as PrimaryKeyAttribute;
                    if (primaryKey != null)
                    {
                        key = primaryKey.GetMappingName();
                    }
                }
            }
            return key;
        }

        /// <summary>
        /// 获取表字段描述名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="curname"></param>
        /// <returns></returns>
        public static string GetColumnName<T>(string curname)
        {
            string key = "未知名称";
            Type type = typeof(T);
            foreach (var pi in type.GetProperties())
            {
                if (pi.Name == curname)
                {
                    var infos = pi.GetCustomAttributes(typeof(NameAttribute), true);
                    foreach (var info in infos)
                    {
                        NameAttribute nameAttribute = info as NameAttribute;
                        if (nameAttribute != null)
                        {
                            key = nameAttribute.GetMappingName();
                        }
                    }
                    break;
                }
            }
            return key;
        }


    }
}
